﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Schedule : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView_Schedule_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (Utility.HasNullValue(e.NewValues))
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "输入不能为空！";
            e.Cancel = true;
            return;
        }

        string strDeadline = e.NewValues["DeadLine"].ToString();
        int deadline;
        if (!int.TryParse(strDeadline, out deadline) || deadline <= 0 || deadline > 31)
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "输入必须在1到31之间！";
            e.Cancel = true;
            return;
        }

        string strCurId = e.OldValues["Id"].ToString();
        int curId;
        if (int.TryParse(strCurId, out curId))
        {
            if (curId > 1)
            {
                GridViewRow row = GridView_Schedule.Rows[curId - 2];
                int preDdeadline;
                if (int.TryParse(row.Cells[2].Text, out preDdeadline) && preDdeadline >= deadline)
                {
                    ((Label)Page.Master.FindControl("ErrorLabel")).Text = "截止日期应为递增！";
                    e.Cancel = true;
                    return;
                }

            }

            if (curId < GridView_Schedule.Rows.Count)
            {
                GridViewRow row = GridView_Schedule.Rows[curId];
                int postDdeadline;
                if (int.TryParse(row.Cells[2].Text, out postDdeadline) && postDdeadline <= deadline)
                {
                    ((Label)Page.Master.FindControl("ErrorLabel")).Text = "截止日期应为递增！";
                    e.Cancel = true;
                    return;
                }

            }
        }
    }
    protected void GridView_Schedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                int cellWidth = 0;
                foreach (object ctrl in cell.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox curText = (TextBox)ctrl;
                        curText.Width = Unit.Pixel(60);
                        cellWidth = 60; 
                    }
                }

                if (cellWidth != 0)
                    cell.Width = Unit.Pixel(cellWidth);
            }
        }
    }
}
